package com.wanxi.service.impl;

import com.wanxi.dao.NavDao;
import com.wanxi.dao.impl.NavDaoImpl;
import com.wanxi.dto.NavDTO;
import com.wanxi.entity.NavEntity;
import com.wanxi.service.NavService;
import com.wanxi.util.Tool;
import com.wanxi.vo.HomeVO;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements NavService {
    private NavDao navDao = new NavDaoImpl();

    @Override
    public List<NavDTO> find(HomeVO homeVO) {
        Jedis jedis = new Jedis();
        jedis.select(10);
        List<NavEntity> entityList = new ArrayList<>();
        boolean isHave = jedis.exists("navTitles");
        if (isHave) {
//从redis里面取值,并存入list
            List<String> navTitleList = jedis.lrange("navTitles", 0, jedis.llen("navTitles"));
            List<String> navHrefList = jedis.lrange("navHrefs", 0, -1);
            for (int i = 0; i < navHrefList.size(); i++) {
                entityList.add(new NavEntity(navTitleList.get(i), navHrefList.get(i)));
            }
            System.out.println("jedis");
        } else {
            //        将vo的值copy到entity
            NavEntity navEntity = Tool.copy(homeVO, NavEntity.class);
//        NavEntity navEntity=new NavEntity();
//        将entity的值copy到dto
            entityList = navDao.find(navEntity);
//            将数据库里面的值,存入redis里面
            for (NavEntity entity : entityList
            ) {
                jedis.rpush("navTitles", entity.getTitle());
                jedis.rpush("navHrefs", entity.getHref());
            }
            System.out.println("mysql");
        }
        return Tool.copy(entityList, NavDTO.class);
    }
}
